package org.jeecg.modules.icon.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.core.constant.DataStatusEnum;
import org.jeecg.modules.core.constant.YesOrNotEnum;
import org.jeecg.modules.icon.entity.BimGeoPic;
import org.jeecg.modules.icon.mapper.BimGeoPicMapper;
import org.jeecg.modules.icon.service.IBimGeoPicService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @Description: bim_geo_icon
 * @Author: jeecg-boot
 * @Date: 2022-12-06
 * @Version: V1.0
 */
@Service
@Slf4j
public class BimGeoPicServiceImpl extends ServiceImpl<BimGeoPicMapper, BimGeoPic> implements IBimGeoPicService {


    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;

    @Value(value = "${jeecg.path.fileUrl}")
    private String fileUrl;


    @Override
    public void add(BimGeoPic bimGeoPic) {
        BimGeoPic oneVerify = this.findOneVerify(bimGeoPic);
        if (oneVerify != null) {
            log.error("添加Icon配置失败，Icon配置已存在！param=>{}", JSON.toJSONString(bimGeoPic));
            throw new JeecgBootException("添加Icon配置失败，Icon配置已存在！");
        }
        //删除状态
        bimGeoPic.setEnableFlag(YesOrNotEnum.Y.getCode());
        bimGeoPic.setDelFlag(DataStatusEnum.NORMAL.getCode());
        this.save(bimGeoPic);
    }

    @Override
    public void update(BimGeoPic bimGeoPic) {
        if (bimGeoPic.getId() == null) {
            log.error("修改Icon配置失败，Id不能为空！！param=>{}", JSON.toJSONString(bimGeoPic));
            throw new JeecgBootException("修改Icon配置失败，Id不能为空！");
        }
        BimGeoPic oneVerify = this.findOneVerify(bimGeoPic);
        if (oneVerify != null && (!oneVerify.getId().equals(bimGeoPic.getId()))) {
            log.error("修改Icon配置失败，Icon配置已存在！param=>{}", JSON.toJSONString(bimGeoPic));
            throw new JeecgBootException("修改Icon配置失败，Icon配置已存在！");
        }
        this.updateById(bimGeoPic);
    }


    @Override
    public String uploadIcon(MultipartFile mf, String bizPath) {
        String dbpath;
        try {
            String ctxPath = uploadpath;
            File file = new File(ctxPath + File.separator + bizPath + File.separator);
            if (!file.exists()) {
                file.mkdirs();// 创建文件根目录
            }
            //文件后缀名
            String suffix = FileUtil.extName(mf.getOriginalFilename());
            String fileName = IdUtil.simpleUUID() + "_" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + "." + suffix;
            String savePath = file.getPath() + File.separator + fileName;
            File saveFile = new File(savePath);
            FileCopyUtils.copy(mf.getBytes(), saveFile);
            dbpath = bizPath + File.separator + fileName;
            if (dbpath.contains("\\")) {
                dbpath = dbpath.replace("\\", "/");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
        return fileUrl + dbpath;
    }

    private BimGeoPic findOneVerify(BimGeoPic bimGeoPic) {
        LambdaUpdateWrapper<BimGeoPic> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(bimGeoPic.getGuid()), BimGeoPic::getGuid, bimGeoPic.getGuid())
                .eq(BimGeoPic::getType, bimGeoPic.getType())// 图标类型
                .eq(BimGeoPic::getIconKind, bimGeoPic.getIconKind())//图标类别
                .eq(BimGeoPic::getName, bimGeoPic.getName());
        List<BimGeoPic> bimGeoPics = this.getBaseMapper().selectList(queryWrapper);
        return CollectionUtils.isEmpty(bimGeoPics) ? null : bimGeoPics.get(0);
    }

}
